<?php
include 'includes/header.php';
include 'includes/classes/task.php';
include 'includes/classes/taskDao.php';
header('Content-type: text/json');
header("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1
header("Expires: Sat, 26 Jul 1997 05:00:00 GMT"); // Date in the past
if (isset($_REQUEST['action'])){
	switch ($_REQUEST['action']){
		case 'login':
			echo login();
			break;
	    case 'newTask':
			echo newTaks();
			break;
        case 'getLast':
			echo getLast();
			break;
        case 'getUser':
			echo getUser();
			break;
        case 'getLastbyUser':
			echo getLastbyUser();
			break;
        case 'findByText':
			echo findByText();
			break;
        case 'findByuser':
			echo findByuser();
			break;
        case 'getTweetCountByUser':
			echo getTweetCountByUser();
			break;  
	}
}
function login(){
    $password = $_REQUEST['password'];
    $username= $_REQUEST['username'];
    
    $query = "select  * from usuario where username='$username' and  password='$password';";
    $result = mysql_query($query);
    
    if (mysql_num_rows($result)==0)
    	return "{logeado:false}";
        
    while ($result && $row = mysql_fetch_array($result)){
		$_SESSION['logeado'] = $row["username"];
        return "{logeado:true}";
	}    
}

function getTweetCountByUser(){
    $username= $_REQUEST['username'];
    
    $query = "select  count(*)  as cantidad from task where username='$username' ;";
    $result = mysql_query($query);
    
    if (mysql_num_rows($result)==0)
    	return "{logeado:false}";
        
    while ($result && $row = mysql_fetch_array($result)){
        $cantidad = $row["cantidad"];
        return "{cantidad:$cantidad}";
	}    
}
function newTaks(){
    $foo = new Task();
    $foo->username = $_REQUEST['username'];
    $foo->descripcion = $_REQUEST['descripcion'];
    $foo->save();
    return $foo->toJson();
}

function getLast(){
    $cantidad = $_REQUEST['items'];
    $lastItemId = $_REQUEST['lastItemId'];
    $TaskDao = new TaskDao();
    if ($lastItemId)
        $a = $TaskDao->getLast($cantidad,$lastItemId);
    else
        $a = $TaskDao->getLast($cantidad);
    return json_encode($a);
}

function getUser(){
    $username = $_REQUEST['username'];
    $query = "select  * 
        from usuario 
        where username='$username'";
    $result = mysql_query($query);
    if ($result){
        $row = mysql_fetch_array($result);
        return json_encode($row);
    }
}

function getLastbyUser(){
    $username = $_REQUEST['username'];
    $TaskDao = new TaskDao();
    $task = $TaskDao->getLastbyUser($username);
    return $task->toJson();
}


function findByText(){
    $text= $_REQUEST['text'];
    $cantidad = $_REQUEST['items'];
    $lastItemId = $_REQUEST['lastItemId'];
    $TaskDao = new TaskDao();
    if ($lastItemId)
        $a = $TaskDao->findByText($text, $cantidad,$lastItemId);
    else
        $a = $TaskDao->findByText($text, $cantidad);
    return json_encode($a);
}
function findByuser(){
    $username= $_REQUEST['username'];
    $cantidad = $_REQUEST['items'];
    $lastItemId = $_REQUEST['lastItemId'];
    $TaskDao = new TaskDao();
    if ($lastItemId)
        $a = $TaskDao->findByuser($username, $cantidad,$lastItemId);
    else
        $a = $TaskDao->findByuser($username, $cantidad);
    return json_encode($a);
}

include 'includes/footer.php';
?>